package br.uefs.ecomp.myBook.model;

import static org.junit.Assert.*;
import br.uefs.ecomp.myBook.util.*;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;

public class ArquivoTest {

	private GeneralFile a, b, c, d, e, f;

	@Before
	public void setUp() throws Exception {
		a = GeneralFile.parse("mybook/files/penguins.jpg");
		b = GeneralFile.parse("mybook/files/texticulo.txt");
		try {
			c = GeneralFile.parse("mybook/files/csvfile.csv");
			fail("Deveria ter falhado.");
		} catch (Exception e) {
		}
		d = GeneralFile.parse("mybook/files/penguins.jpg");
		e = GeneralFile.parse("mybook/files/out.bmp");
		f = GeneralFile.parse("mybook/files/sample.mp3");
	}

	@Test
	public void testLoads() {
		assertNotNull(a);
		assertNotNull(b);
		assertNull(c);
	}

	@Test
	public void imageFilesTest() {
		assertTrue(a instanceof ImageFile);
		assertTrue(e instanceof ImageFile);
		ImageFile newA, newE;
		newA = (ImageFile) a;
		newE = (ImageFile) e;
		assertEquals(newA.getWidth(), 1024);
		assertEquals(newA.getHeight(), 768);
		assertEquals(newE.getWidth(), 1024);
		assertEquals(newE.getHeight(), 768);
	}

	@Test
	public void textFilesTest() {
		assertTrue(b instanceof TextFile);
		TextFile newB = (TextFile) b;
		assertEquals(23, newB.getWordCount());
		assertEquals(112, newB.getCharCount());
	}

	@Test
	public void audioFilesTest() {
		assertTrue(f instanceof AudioFile);
		AudioFile newF = (AudioFile) f;
		assertEquals("Save the world", newF.getTitle());
		assertEquals("Summereletrhits 8", newF.getAlbum());
		assertEquals("00:06:51", newF.getLength());
	}

	@Test
	public void testCompareTo() {
		assertTrue(a.compareTo(b) < 0);
		assertTrue(a.compareTo(d) == 0);
		assertTrue(b.compareTo(d) > 0);
	}

	@Test
	public void testLoadDirectory() throws IOException,
			DataAlreadyExistsException {
		GeneralFile[] arqs = GeneralFile.listDirectory("mybook/files/2/");
		AVLTree tree = new AVLTree();
		String tempString = "";
		for (GeneralFile a : arqs)
			tree.add(a);
		for (Object o : tree)
			tempString += o.toString() + "\n";
		String tempString2 = e.toString() + "\n" + d.toString() + "\n"
				+ b.toString() + "\n";
		assertEquals(tempString2, tempString);
	}

}
